<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - coverage.lcov - src/mqtt_client.dart</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - mqtt_client.dart<span style="font-size: 80%;"> (source / <a href="mqtt_client.dart.func.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">coverage.lcov</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">31</td>
            <td class="headerCovTableEntry">58</td>
            <td class="headerCovTableEntryLo">53.4 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2017-10-09</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntryHi">-</td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /*</a>
<span class="lineNum">       2 </span>            :  * Package : mqtt_client
<span class="lineNum">       3 </span>            :  * Author : S. Hamblett &lt;steve.hamblett@linux.com&gt;
<span class="lineNum">       4 </span>            :  * Date   : 10/07/2017
<span class="lineNum">       5 </span>            :  * Copyright :  S.Hamblett
<span class="lineNum">       6 </span>            :  */
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : part of mqtt_client;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : /// A client class for interacting with MQTT Data Packets
<span class="lineNum">      11 </span>            : class MqttClient {
<span class="lineNum">      12 </span>            :   /// Initializes a new instance of the MqttClient class using the default Mqtt Port.
<span class="lineNum">      13 </span>            :   /// The server hostname to connect to
<span class="lineNum">      14 </span>            :   /// The client identifier to use to connect with
<span class="lineNum">      15 </span><span class="lineCov">          2 :   MqttClient(this.server, this.clientIdentifier) {</span>
<span class="lineNum">      16 </span><span class="lineCov">          2 :     this.port = Constants.defaultMqttPort;</span>
<span class="lineNum">      17 </span>            :   }
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span>            :   /// Initializes a new instance of the MqttClient class using the supplied Mqtt Port.
<span class="lineNum">      20 </span>            :   /// The server hostname to connect to
<span class="lineNum">      21 </span>            :   /// The client identifier to use to connect with
<span class="lineNum">      22 </span>            :   /// The port to use
<span class="lineNum">      23 </span><span class="lineNoCov">          0 :   MqttClient.withPort(this.server, this.clientIdentifier, this.port);</span>
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            :   String server;
<span class="lineNum">      26 </span>            :   int port;
<span class="lineNum">      27 </span>            :   String clientIdentifier;
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span>            :   /// If set use a websocket connection, otherwise use the default TCP one
<span class="lineNum">      30 </span>            :   bool useWebSocket = false;
<span class="lineNum">      31 </span>            : 
<span class="lineNum">      32 </span>            :   /// If set use a secure connection, note TCP only, not websocket.
<span class="lineNum">      33 </span>            :   bool secure = false;
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            :   /// Trusted certificate file path for use in secure working
<span class="lineNum">      36 </span>            :   String trustedCertPath;
<span class="lineNum">      37 </span>            : 
<span class="lineNum">      38 </span>            :   /// Private key file path
<span class="lineNum">      39 </span>            :   String privateKeyFilePath;
<span class="lineNum">      40 </span>            : 
<span class="lineNum">      41 </span>            :   /// The Handler that is managing the connection to the remote server.
<span class="lineNum">      42 </span>            :   MqttConnectionHandler _connectionHandler;
<span class="lineNum">      43 </span>            : 
<span class="lineNum">      44 </span>            :   /// The subscriptions manager responsible for tracking subscriptions.
<span class="lineNum">      45 </span>            :   SubscriptionsManager _subscriptionsManager;
<span class="lineNum">      46 </span>            : 
<span class="lineNum">      47 </span>            :   /// Handles the connection management while idle.
<span class="lineNum">      48 </span>            :   MqttConnectionKeepAlive _keepAlive;
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            :   /// Keep alive period, seconds
<span class="lineNum">      51 </span>            :   int keepAlivePeriod = Constants.defaultKeepAlive;
<span class="lineNum">      52 </span>            : 
<span class="lineNum">      53 </span>            :   /// Handles everything to do with publication management.
<span class="lineNum">      54 </span>            :   PublishingManager _publishingManager;
<span class="lineNum">      55 </span>            : 
<span class="lineNum">      56 </span>            :   /// Gets the current conneciton state of the Mqtt Client.
<span class="lineNum">      57 </span>            :   ConnectionState get connectionState =&gt;
<span class="lineNum">      58 </span><span class="lineCov">          1 :       _connectionHandler != null</span>
<span class="lineNum">      59 </span><span class="lineCov">          2 :           ? _connectionHandler.connectionState</span>
<span class="lineNum">      60 </span>            :           : ConnectionState.disconnected;
<span class="lineNum">      61 </span>            : 
<span class="lineNum">      62 </span>            :   /// The connection message to use to override the default
<span class="lineNum">      63 </span>            :   MqttConnectMessage connectionMessage;
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span>            :   /// Performs a synchronous connect to the message broker with an optional username and password
<span class="lineNum">      66 </span>            :   /// for the purposes of authentication.
<span class="lineNum">      67 </span>            :   Future&lt;ConnectionState&gt; connect([String username, String password]) async {
<span class="lineNum">      68 </span>            :     if (username != null) {
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :       print(</span>
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :           &quot;Authenticating with username '{$username}' and password '{$password}'&quot;);</span>
<span class="lineNum">      71 </span>            :       if (username
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :           .trim()</span>
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :           .length &gt;</span>
<span class="lineNum">      74 </span>            :           Constants.recommendedMaxUsernamePasswordLength) {
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :         print(&quot;Username length (${username</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :                 .trim()</span>
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :                 .length}) exceeds the max recommended in the MQTT spec. &quot;);</span>
<span class="lineNum">      78 </span>            :       }
<span class="lineNum">      79 </span>            :     }
<span class="lineNum">      80 </span>            :     if (password != null &amp;&amp;
<span class="lineNum">      81 </span>            :         password
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :             .trim()</span>
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :             .length &gt;</span>
<span class="lineNum">      84 </span>            :             Constants.recommendedMaxUsernamePasswordLength) {
<span class="lineNum">      85 </span><span class="lineNoCov">          0 :       print(&quot;Password length (${ password</span>
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :               .trim()</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :               .length}) exceeds the max recommended in the MQTT spec. &quot;);</span>
<span class="lineNum">      88 </span>            :     }
<span class="lineNum">      89 </span><span class="lineCov">          2 :     _connectionHandler = new SynchronousMqttConnectionHandler();</span>
<span class="lineNum">      90 </span><span class="lineCov">          1 :     if (useWebSocket) {</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :       _connectionHandler.secure = false;</span>
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :       _connectionHandler.useWebSocket = true;</span>
<span class="lineNum">      93 </span>            :     }
<span class="lineNum">      94 </span><span class="lineCov">          1 :     if (secure) {</span>
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :       _connectionHandler.secure = true;</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :       _connectionHandler.useWebSocket = false;</span>
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :       _connectionHandler.trustedCertPath = trustedCertPath;</span>
<span class="lineNum">      98 </span><span class="lineNoCov">          0 :       _connectionHandler.privateKeyFilePath = privateKeyFilePath;</span>
<span class="lineNum">      99 </span>            :     }
<span class="lineNum">     100 </span><span class="lineCov">          3 :     _publishingManager = new PublishingManager(_connectionHandler);</span>
<span class="lineNum">     101 </span><span class="lineCov">          1 :     _subscriptionsManager =</span>
<span class="lineNum">     102 </span><span class="lineCov">          3 :     new SubscriptionsManager(_connectionHandler, _publishingManager);</span>
<span class="lineNum">     103 </span><span class="lineCov">          2 :     _keepAlive = new MqttConnectionKeepAlive(</span>
<span class="lineNum">     104 </span><span class="lineCov">          2 :         _connectionHandler, keepAlivePeriod);</span>
<span class="lineNum">     105 </span><span class="lineCov">          1 :     final connectMessage = _getConnectMessage(username, password);</span>
<span class="lineNum">     106 </span><span class="lineCov">          3 :     return await _connectionHandler.connect(</span>
<span class="lineNum">     107 </span><span class="lineCov">          2 :         this.server, this.port, connectMessage);</span>
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :   }</span>
<span class="lineNum">     109 </span>            : 
<span class="lineNum">     110 </span>            :   ///  Gets a pre-configured connect message if one has not been supplied by the user.
<span class="lineNum">     111 </span>            :   ///  Returns an MqttConnectMessage that can be used to connect to a message broker
<span class="lineNum">     112 </span>            :   MqttConnectMessage _getConnectMessage(String username, String password) {
<span class="lineNum">     113 </span><span class="lineCov">          1 :     if (connectionMessage == null) {</span>
<span class="lineNum">     114 </span><span class="lineCov">          2 :       connectionMessage = new MqttConnectMessage()</span>
<span class="lineNum">     115 </span><span class="lineCov">          2 :           .withClientIdentifier(clientIdentifier)</span>
<span class="lineNum">     116 </span><span class="lineCov">          1 :           .keepAliveFor(Constants.defaultKeepAlive)</span>
<span class="lineNum">     117 </span><span class="lineCov">          1 :           .authenticateAs(username, password)</span>
<span class="lineNum">     118 </span><span class="lineCov">          1 :           .startClean();</span>
<span class="lineNum">     119 </span>            :     }
<span class="lineNum">     120 </span><span class="lineCov">          1 :     return connectionMessage;</span>
<span class="lineNum">     121 </span>            :   }
<span class="lineNum">     122 </span>            : 
<span class="lineNum">     123 </span>            :   /// Initiates a topic subscription request to the connected broker with a strongly typed data processor callback.
<span class="lineNum">     124 </span>            :   /// The topic to subscribe to.
<span class="lineNum">     125 </span>            :   /// The qos level the message was published at.
<span class="lineNum">     126 </span>            :   /// Returns the change notifier assigned to the subscription.
<span class="lineNum">     127 </span>            :   /// Raises InvalidTopicException If a topic that does not meet the MQTT topic spec rules is provided.
<span class="lineNum">     128 </span>            :   ChangeNotifier&lt;MqttReceivedMessage&gt; listenTo(String topic, MqttQos qosLevel) {
<span class="lineNum">     129 </span><span class="lineNoCov">          0 :     if (_connectionHandler.connectionState != ConnectionState.connected) {</span>
<span class="lineNum">     130 </span><span class="lineNoCov">          0 :       throw new ConnectionException(_connectionHandler.connectionState);</span>
<span class="lineNum">     131 </span>            :     }
<span class="lineNum">     132 </span><span class="lineNoCov">          0 :     return _subscriptionsManager.registerSubscription(topic, qosLevel);</span>
<span class="lineNum">     133 </span>            :   }
<span class="lineNum">     134 </span>            : 
<span class="lineNum">     135 </span>            :   /// Publishes a message to the message broker.
<span class="lineNum">     136 </span>            :   /// Returns The message identifer assigned to the message.
<span class="lineNum">     137 </span>            :   /// Raises InvalidTopicException if the topic supplied violates the MQTT topic format rules.
<span class="lineNum">     138 </span>            :   int publishMessage(String topic, MqttQos qualityOfService,
<span class="lineNum">     139 </span>            :       typed.Uint8Buffer data) {
<span class="lineNum">     140 </span><span class="lineCov">          3 :     if (_connectionHandler.connectionState != ConnectionState.connected) {</span>
<span class="lineNum">     141 </span><span class="lineNoCov">          0 :       throw new ConnectionException(_connectionHandler.connectionState);</span>
<span class="lineNum">     142 </span>            :     }
<span class="lineNum">     143 </span>            :     try {
<span class="lineNum">     144 </span><span class="lineCov">          1 :       final PublicationTopic pubTopic = new PublicationTopic(topic);</span>
<span class="lineNum">     145 </span><span class="lineCov">          2 :       return _publishingManager.publish(pubTopic, qualityOfService, data);</span>
<span class="lineNum">     146 </span>            :     } catch (Exception) {
<span class="lineNum">     147 </span><span class="lineNoCov">          0 :       throw new InvalidTopicException(Exception.toString(), topic);</span>
<span class="lineNum">     148 </span>            :     }
<span class="lineNum">     149 </span>            :   }
<span class="lineNum">     150 </span>            : 
<span class="lineNum">     151 </span>            :   /// Unsubscribe from a topic
<span class="lineNum">     152 </span>            :   void unsubscribe(String topic) {
<span class="lineNum">     153 </span><span class="lineNoCov">          0 :     _subscriptionsManager.unsubscribe(topic);</span>
<span class="lineNum">     154 </span>            :   }
<span class="lineNum">     155 </span>            : 
<span class="lineNum">     156 </span>            :   /// Gets the current status of a subscription.
<span class="lineNum">     157 </span>            :   SubscriptionStatus getSubscriptionsStatus(String topic) {
<span class="lineNum">     158 </span><span class="lineNoCov">          0 :     return _subscriptionsManager.getSubscriptionsStatus(topic);</span>
<span class="lineNum">     159 </span>            :   }
<span class="lineNum">     160 </span>            : 
<span class="lineNum">     161 </span>            :   /// Disconnect from the broker
<span class="lineNum">     162 </span>            :   void disconnect() {
<span class="lineNum">     163 </span><span class="lineCov">          2 :     _connectionHandler.disconnect();</span>
<span class="lineNum">     164 </span><span class="lineCov">          1 :     _publishingManager = null;</span>
<span class="lineNum">     165 </span><span class="lineCov">          1 :     _subscriptionsManager = null;</span>
<span class="lineNum">     166 </span><span class="lineCov">          2 :     _keepAlive.stop();</span>
<span class="lineNum">     167 </span><span class="lineCov">          1 :     _keepAlive = null;</span>
<span class="lineNum">     168 </span><span class="lineCov">          1 :     _connectionHandler = null;</span>
<span class="lineNum">     169 </span>            :   }
<span class="lineNum">     170 </span>            : 
<span class="lineNum">     171 </span>            :   /// Turn on logging, true to start, false to stop
<span class="lineNum">     172 </span>            :   void logging(bool on) {
<span class="lineNum">     173 </span>            :     MqttLogger.loggingOn = false;
<span class="lineNum">     174 </span>            :     if (on) {
<span class="lineNum">     175 </span>            :       MqttLogger.loggingOn = true;
<span class="lineNum">     176 </span>            :     }
<span class="lineNum">     177 </span>            :   }
<span class="lineNum">     178 </span>            : 
<span class="lineNum">     179 </span>            :   /// Set the protocol version to V3.1 - default
<span class="lineNum">     180 </span>            :   void setProtocolV31() {
<span class="lineNum">     181 </span>            :     Protocol.version = Constants.mqttV31ProtocolVersion;
<span class="lineNum">     182 </span>            :     Protocol.name = Constants.mqttV31ProtocolName;
<span class="lineNum">     183 </span>            :   }
<span class="lineNum">     184 </span>            : 
<span class="lineNum">     185 </span>            :   /// Set the protocol version to V3.1.1
<span class="lineNum">     186 </span>            :   void setProtocolV311() {
<span class="lineNum">     187 </span>            :     Protocol.version = Constants.mqttV311ProtocolVersion;
<span class="lineNum">     188 </span>            :     Protocol.name = Constants.mqttV311ProtocolName;
<span class="lineNum">     189 </span>            :   }
<span class="lineNum">     190 </span>            : }
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.10</a></td></tr>
  </table>
  <br>

</body>
</html>
